package pl.un4giv3n.mightvsmagic.entity;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 * @author andrzejewskia this is a concrete item, created from the item
 *         template, bought on bazar by the player
 */
@NamedQueries({
	@NamedQuery(name="find.template.items", query="select i from Item i where i.templateId = :templateId")
})
@Entity
public class Item implements Comparable<Item>{
	/**
	 * id
	 */
	private Long id;
	/**
	 * item name
	 */
	private String name;
	/**
	 * item type
	 */
	@Enumerated(EnumType.STRING)
	private ItemType type;
	/**
	 * item image url
	 */
	private String imgUrl;
	/**
	 * item min def
	 */
	private double minDef;
	/**
	 * item max def
	 */
	private double maxDef;
	/**
	 * item min dmg
	 */
	private double minDmg;
	/**
	 * item max dmg
	 */
	private double maxDmg;
	/**
	 * item price
	 */
	private int price;
	private int premiumPrice;
	/**
	 * item modificatiors
	 */
	private long strenghtModifier;
	private long staminaModifier;
	private long manaModifier;
	private long attackModifier;
	private long parryModifier;
	private long speedModifier;
	/**
	 * item requirements
	 */
	private long strenghtReq;
	private long staminaReq;
	private long manaReq;
	private long attackReq;
	private long parryReq;
	private long requiredFightExp;
	private long requiredMagicExp;

	/**
	 * is the item in use
	 */
	private boolean inUse;
	private boolean sharpened = false;
	private Long templateId;
	private long requiredAnyExp;

	@SuppressWarnings("unused")
	private Item() {
		super();
	}

	public Item(String name, ItemType type, String imgUrl, double minDef,
			double maxDef, double minDmg, double maxDmg, int price, int premiumPrice,
			long requiredFightExp, long requiredMagicExp, long requiredAnyExp,
			long strenghtModifier, long staminaModifier, long manaModifier,
			long attackModifier, long parryModifier, long speedModifier, long strenghtReq,
			long staminaReq, long manaReq, long attackReq, long parryReq, Long templateId) {
		this.name = name;
		this.type = type;
		this.imgUrl = imgUrl;
		this.minDef = minDef;
		this.maxDef = maxDef;
		this.minDmg = minDmg;
		this.maxDmg = maxDmg;
		this.price = price;
		this.premiumPrice = premiumPrice;
		this.requiredFightExp = requiredFightExp;
		this.requiredMagicExp = requiredMagicExp;
		this.requiredAnyExp = requiredAnyExp;
		this.strenghtModifier = strenghtModifier;
		this.staminaModifier = staminaModifier;
		this.manaModifier = manaModifier;
		this.attackModifier = attackModifier;
		this.parryModifier = parryModifier;
		this.speedModifier = speedModifier;
		this.strenghtReq = strenghtReq;
		this.staminaReq = staminaReq;
		this.manaReq = manaReq;
		this.attackReq = attackReq;
		this.parryReq = parryReq;
		this.templateId = templateId;
	}

	public boolean isInUse() {
		return inUse;
	}

	public void setInUse(boolean inUse) {
		this.inUse = inUse;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ItemType getType() {
		return type;
	}

	public void setType(ItemType type) {
		this.type = type;
	}

	public String getImgUrl() {
		return imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

	public double getMinDef() {
		return minDef;
	}

	public void setMinDef(double minDef) {
		this.minDef = minDef;
	}

	public double getMaxDef() {
		return maxDef;
	}

	public void setMaxDef(double maxDef) {
		this.maxDef = maxDef;
	}

	public double getMinDmg() {
		return minDmg;
	}

	public void setMinDmg(double minDmg) {
		this.minDmg = minDmg;
	}

	public double getMaxDmg() {
		return maxDmg;
	}

	public void setMaxDmg(double maxDmg) {
		this.maxDmg = maxDmg;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	void setId(Long id) {
		this.id = id;
	}

	public long getRequiredFightExp() {
		return requiredFightExp;
	}

	public void setRequiredFightExp(long requiredFightExp) {
		this.requiredFightExp = requiredFightExp;
	}

	public long getRequiredMagicExp() {
		return requiredMagicExp;
	}

	public void setRequiredMagicExp(long requiredMagicExp) {
		this.requiredMagicExp = requiredMagicExp;
	}

	public long getStrenghtModifier() {
		return strenghtModifier;
	}

	public void setStrenghtModifier(long strenghtModifier) {
		this.strenghtModifier = strenghtModifier;
	}

	public long getStaminaModifier() {
		return staminaModifier;
	}

	public void setStaminaModifier(long staminaModifier) {
		this.staminaModifier = staminaModifier;
	}

	public long getManaModifier() {
		return manaModifier;
	}

	public void setManaModifier(long manaModifier) {
		this.manaModifier = manaModifier;
	}

	public long getAttackModifier() {
		return attackModifier;
	}

	public void setAttackModifier(long attackModifier) {
		this.attackModifier = attackModifier;
	}

	public long getParryModifier() {
		return parryModifier;
	}

	public void setParryModifier(long parryModifier) {
		this.parryModifier = parryModifier;
	}

	public long getStrenghtReq() {
		return strenghtReq;
	}

	public void setStrenghtReq(long strenghtReq) {
		this.strenghtReq = strenghtReq;
	}

	public long getStaminaReq() {
		return staminaReq;
	}

	public void setStaminaReq(long staminaReq) {
		this.staminaReq = staminaReq;
	}

	public long getManaReq() {
		return manaReq;
	}

	public void setManaReq(long manaReq) {
		this.manaReq = manaReq;
	}

	public long getAttackReq() {
		return attackReq;
	}

	public void setAttackReq(long attackReq) {
		this.attackReq = attackReq;
	}

	public long getParryReq() {
		return parryReq;
	}

	public void setParryReq(long parryReq) {
		this.parryReq = parryReq;
	}

	public Long getTemplateId() {
		return templateId;
	}

	public void setTemplateId(Long templateId) {
		this.templateId = templateId;
	}

	public int getPremiumPrice() {
		return premiumPrice;
	}

	public void setPremiumPrice(int premiumPrice) {
		this.premiumPrice = premiumPrice;
	}

	@Override
	public int compareTo(Item o) {
		long thisVal = this.requiredAnyExp;
		long anotherVal = o.requiredAnyExp;
		if(thisVal == 0 && anotherVal == 0){
			thisVal = this.requiredFightExp + this.requiredMagicExp;
			anotherVal = o.requiredFightExp + o.requiredMagicExp;
		}
		if(thisVal == 0 && anotherVal == 0){
			thisVal = attackReq + parryReq + manaReq + staminaReq + strenghtReq + requiredFightExp + requiredMagicExp;
			anotherVal = o.attackReq + o.parryReq + o.manaReq + o.staminaReq + o.strenghtReq + o.requiredFightExp + o.requiredMagicExp;
		}
		return (thisVal<anotherVal ? 1 : (thisVal==anotherVal ? 0 : -1));
	}

	public boolean isSharpened() {
		return sharpened;
	}

	public void setSharpened(boolean sharpened) {
		this.sharpened = sharpened;
	}

	public long getSpeedModifier() {
		return speedModifier;
	}

	public void setSpeedModifier(long speedModifier) {
		this.speedModifier = speedModifier;
	}

	public void setRequiredAnyExp(long requiredAnyExp) {
		this.requiredAnyExp =  requiredAnyExp;
	}

	public long getRequiredAnyExp() {
		return requiredAnyExp;
	}
	
	@Override
	public String toString() {
		return "Id="+id+", name="+name+", templateId"+templateId+", sharpened="+sharpened;
	}

}
